<?php
/**
 * YAHOO搜索风格的JS分页类
 *
 * @since 0.1.1
 * @package net.4kiki.pager.js
 */
class IYahooJsPager extends IAbstractPager implements IJsPager {
	private $jsFunc;
	
	/**
	 * 设置JS回调函数
	 *
	 * @param string $func 用户传回当前页数的JS函数
	 * @since 0.1.1
	 */	
	public function setJsFunc($func) {
		$this->jsFunc = $func;
	}
	
	/**
	 * 转换为字符串
	 *
	 * @return string
	 */
	public function __toString() {
        $pages = array();
        $pageNum = $this->getPageNumber();
        $currPageNo = $this->getCurrentPageNo();
        $query = $this->getQuery();
        $pageSize = $this->getPageSize();
        $total = $this->getContentSize();
        $pageSetNum = 11;
        $middlePageNum = ceil($pageSetNum/2);
		if ($pageNum > 0) {
			if ($currPageNo <= $middlePageNum) {
				$start = 1;
				$end = min($pageNum, $pageSetNum);
			}
			else if ($currPageNo + $middlePageNum - 1 > $pageNum) {
				$start = max(1, $pageNum - $pageSetNum - 1);
				$end = $pageNum;
			}
			else {
				$start = max(1, $currPageNo - $middlePageNum);
				$end = min($currPageNo + $middlePageNum - 1, $pageNum);
			}
			if ($currPageNo > 1) {
				$pages[] = "<a href=\"#\" onclick=\"{$this->jsFunc}(" . ($currPageNo - 1)  . ",this);return false;\" title=\"Previous {$pageSize}\" class=\"prev_page\">上一页</a>&nbsp; ";
			}
			for ($i = $start; $i <= $end; $i++) {
				$_start = $pageSize * ($i - 1) + 1;
				$_end = min($pageSize * $i, $total);;
				if ($i != $currPageNo) {
					$pages[] = "<a href=\"#\" onclick=\"{$this->jsFunc}(" . ($i) . ",this);return false;\" title=\"Results {$_start} - {$_end}\">{$i}</a>";
				}
				else {
					$pages[] = "<span><a href=\"#\" onclick=\"{$this->jsFunc}(" . ($i) . ",this);return false;\" title=\"Results {$_start} - {$_end}\">{$i}</a></span>";
				}
			}
			if ($currPageNo < $pageNum) {
				$pages[] = " &nbsp;<a href=\"#\" onclick=\"{$this->jsFunc}(" . ($currPageNo + 1) . ",this);return false;\" title=\"Next {$pageSize}\" class=\"next_page\">下一页</a>";
			}
		}
		
       	$string = implode(" &nbsp;", $pages);
        
        return $string;
	}
}

?>